from MyTT import *  # myTT麦语言工具函数指标库  https://github.com/mpquant/MyTT


def sqjz_signal(N, C):
    '''
    参数
        N:到最后交易的周期
        C:当前周期收盘价
    返回值:
        一个dict:
        key:B1,B2,B8,B9;
            S1,S2,S8,S9
    '''
    # 收盘价<4日前的收盘价
    B = C < REF(C, 4);
    # 条件连续成立次数
    T1 = BARSLASTCOUNT(B);
    A_B1 = (T1 > 9) and MOD(T1, 9) == 1;
    A_B2 = (T1 > 9) and MOD(T1, 9) == 2;
    A_B8 = (T1 > 9) and MOD(T1, 9) == 8;
    A_B9 = (T1 > 9) and MOD(T1, 9) == 0;
    B1 = (N == 6 and REFXV(COUNT(B, 6), 5) == 6) or (N == 7 and REFXV(COUNT(B, 7), 6) == 7) or (
                N == 8 and REFXV(COUNT(B, 8), 7) == 8) or (N >= 9 and REFXV(COUNT(B, 9), 8) == 9);
    # 当满足条件B1 AND (1日前的B==0 OR A_B1)时,在最低价位置书写数字,画洋红色
    # DRAWNUMBER(B1 and (REF(B,1)==0 or A_B1),L,1),COLORMAGENTA;
    B2 = (N == 5 and REFXV(COUNT(B, 6), 4) == 6) or (N == 6 and REFXV(COUNT(B, 7), 5) == 7) or (
                N == 7 and REFXV(COUNT(B, 8), 6) == 8) or (N >= 8 and REFXV(COUNT(B, 9), 7) == 9);
    # 当满足条件B2 AND (2日前的B==0 OR A_B2)时,在最低价位置书写数字,画洋红色
    # DRAWNUMBER(B2 and(REF(B,2)==0 or A_B2),L,2),COLORMAGENTA;
    B8 = (N == 1 and COUNT(B, 8) == 8) or (N >= 2 and REFXV(COUNT(B, 9), 1) == 9);
    # 当满足条件B8 AND (8日前的B==0 OR A_B8)时,在最低价位置书写数字,画洋红色
    # DRAWNUMBER(B8 and (REF(B,8)==0 or A_B8),L,8),COLORMAGENTA;
    B9 = (N >= 1 and COUNT(B, 9) == 9);
    # 当满足条件B9 AND (9日前的B==0 OR A_B9)时,在最低价位置书写数字,画红色
    # DRAWNUMBER(B9 and (REF(B,9)==0 or A_B9),L,9),COLORRED;

    # 收盘价>4日前的收盘价
    S = C > REF(C, 4);
    T2 = BARSLASTCOUNT(S);
    A_S1 = (T2 > 9) and MOD(T2, 9) == 1;
    A_S2 = (T2 > 9) and MOD(T2, 9) == 2;
    A_S8 = (T2 > 9) and MOD(T2, 9) == 8;
    A_S9 = (T2 > 9) and MOD(T2, 9) == 0;
    S1 = (N == 6 and REFXV(COUNT(S, 6), 5) == 6) or (N == 7 and REFXV(COUNT(S, 7), 6) == 7) or (
                N == 8 and REFXV(COUNT(S, 8), 7) == 8) or (N >= 9 and REFXV(COUNT(S, 9), 8) == 9);
    # DRAWNUMBER(S1 and (REF(S,1)==0 or A_S1),H,1),COLORMAGENTA,DRAWABOVE;
    S2 = (N == 5 and REFXV(COUNT(S, 6), 4) == 6) or (N == 6 and REFXV(COUNT(S, 7), 5) == 7) or (
                N == 7 and REFXV(COUNT(S, 8), 6) == 8) or (N >= 8 and REFXV(COUNT(S, 9), 7) == 9);
    # DRAWNUMBER(S2 and (REF(S,2)==0 or A_S2),H,2),COLORMAGENTA,DRAWABOVE;
    S8 = (N == 1 and COUNT(S, 8) == 8) or (N >= 2 and REFXV(COUNT(S, 9), 1) == 9);
    # DRAWNUMBER(S8 and (REF(S,8)==0 or A_S8),H,8),COLORMAGENTA,DRAWABOVE;
    S9 = (N >= 1 and COUNT(S, 9) == 9);
    # DRAWNUMBER(S9 and (REF(S,9)==0 or A_S9),H,9),COLORGREEN,DRAWABOVE;   
    return {
        'B1': B1, 'B2': B2, 'B8': B8, 'B9': B9, 'S1': S1, 'S2': S2, 'S8': S8, 'S9': S9
    }
